package com.kevin.netty.demo.packet;

import io.netty.buffer.*;
import io.netty.util.CharsetUtil;

public class ByteBufExample {

    public static void main(String[] args) {

//        createDemo();
//
//        writeDemo();

//        readDemo();
//
//        markReaderDemo();
//        sliceDemo();
//        compositeByteBufDemo();
//        wrapperBufDemo();
        copiedBufDemo();
    }

    private static void copiedBufDemo() {
        System.out.println("copiedBufDemo--------");
        ByteBuf first = ByteBufAllocator.DEFAULT.buffer();
        first.writeCharSequence("12345", CharsetUtil.UTF_8);

        ByteBuf end = ByteBufAllocator.DEFAULT.buffer();
        end.writeCharSequence("678", CharsetUtil.UTF_8);

        ByteBuf byteBuf = Unpooled.copiedBuffer(first,end);
        log(byteBuf);

        log(first);
        log(byteBuf);


    }


    private static void wrapperBufDemo(){
        System.out.println("wrapperBufDemo--------");
        ByteBuf first = ByteBufAllocator.DEFAULT.buffer();
        first.writeCharSequence("header", CharsetUtil.UTF_8);

        ByteBuf end = ByteBufAllocator.DEFAULT.buffer();
        end.writeCharSequence("end", CharsetUtil.UTF_8);

        ByteBuf byteBuf = Unpooled.wrappedBuffer(first,end);
        log(byteBuf);

    }

    private static void compositeByteBufDemo(){
        System.out.println("compositeByteBufDemo--------");
        ByteBuf first = ByteBufAllocator.DEFAULT.buffer();
        first.writeCharSequence("header", CharsetUtil.UTF_8);

        ByteBuf end = ByteBufAllocator.DEFAULT.buffer();
        end.writeCharSequence("end", CharsetUtil.UTF_8);

        CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer();
        compositeByteBuf.addComponents(true,first,end);
        log(compositeByteBuf);

    }


    private static void sliceDemo(){
        System.out.println("sliceDemo--------");
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
        byteBuf.writeBytes(new byte[]{1,2,3,4,5});
        log(byteBuf);
        ByteBuf byteBuf1 = byteBuf.slice(0,2);
        ByteBuf byteBuf2 = byteBuf.slice(2,3);
        log(byteBuf1);
        log(byteBuf2);
        System.out.println("修改数据后---");
        byteBuf.setByte(1,9);
        log(byteBuf1);
    }


    private static void markReaderDemo(){
        System.out.println("readDemo--------");
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
        byteBuf.writeBytes(new byte[]{1,2,3,4,5});
        log(byteBuf);
        System.out.println(byteBuf.readByte());
        byteBuf.markReaderIndex();
        System.out.println(byteBuf.readByte());
        log(byteBuf);
        System.out.println(byteBuf.getByte(0));
        byteBuf.resetReaderIndex();

    }

    private static void readDemo(){
        System.out.println("readDemo--------");
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
        byteBuf.writeBytes(new byte[]{1,2,3,4,5});
        log(byteBuf);
        System.out.println(byteBuf.readByte());
        System.out.println(byteBuf.readByte());
        log(byteBuf);
    }

    private static void writeDemo() {
        System.out.println("write demo-------");
        ByteBuf byteBuf2 = ByteBufAllocator.DEFAULT.buffer();
        byteBuf2.writeBytes(new byte[]{1,2,3,4});
        log(byteBuf2);
        byteBuf2.writeInt(5);
        System.out.println("after---------");
        log(byteBuf2);
    }

    private static void createDemo() {
        System.out.println("create demo-------");
        ByteBuf byteBuf = ByteBufAllocator.DEFAULT.buffer();
        log(byteBuf);

        StringBuilder sb =new StringBuilder();
        for (int i=0;i<10;i++) {
            sb.append(i);
        }

        System.out.println("after------------");
        byteBuf.writeBytes(sb.toString().getBytes());
        log(byteBuf);
    }

    private static void log(ByteBuf bytebuf){
        StringBuilder sb = new StringBuilder();
        sb.append("read index:").append(bytebuf.readerIndex())
                .append("write index:").append(bytebuf.writerIndex())
                .append("capacity:").append(bytebuf.capacity());
        ByteBufUtil.appendPrettyHexDump(sb,bytebuf);
        System.out.println(sb.toString());
    }
}
